﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Common;
using Common.Entities;
using OrderSales.Interface;
using OrderSales.Presenter;

namespace OrderSales
{
    public partial class OrderForm : Form, IOrderForm
    {
        #region IOrderForm Members

        public string NumberOrder
        {
            get
            {
                return numberOrderTextBox.Text;
            }
            set
            {
                numberOrderTextBox.Text = value;
            }
        }

        public DateTime DateOrder
        {
            get
            {
                return dateOrderDateTimePicker.Value;
            }
            set
            {
                dateOrderDateTimePicker.Value = value;
            }
        }

        public double Amount
        {
            set { totalAmountLabel.Text = String.Format("Сума {0} грн.", value.ToString()); }
        }

        public List<OrderRecord> RecordOrders
        {
            set
            {
                orderRecordDataGrid.DataSource = value;
            }
        }

        public object DataSource
        {
            set
            {
                orderRecordDataGrid.DataSource = value;
                recordOrderdataGridTableStyle.MappingName = orderRecordDataGrid.DataSource.GetType().Name;
            }
            get
            {
                return orderRecordDataGrid.DataSource;
            }
        }

        public object Current
        {
            get
            {
                return orderRecordDataGrid.CurrentRowIndex;
            }
        }

        public event EventHandler<EventArgs> AddRecordOrder;

        public event EventHandler<EventArgs> RemoveRecordOrder;

        public event EventHandler<EventArgs> ChageNumber;

        public event EventHandler<EventArgs> ChageDate;


        #endregion

        public OrderForm(Order order)
        {
            InitializeComponent();
            new OrderFormPresenter(order, this);
            orderRecordDataGrid.RowHeadersVisible = false;
        }

        private void addGoodMenuItem_Click(object sender, EventArgs e)
        {
            AddRecordOrder(null, null);
        }

       private void orderRecordDataGrid_CurrentCellChanged(object sender, EventArgs e)
        {
            if (orderRecordDataGrid.CurrentRowIndex >=0)
                removeGoodMenuItem.Enabled = true;
            else
            {
                removeGoodMenuItem.Enabled = false;
            }
        }

        private void removeGoodMenuItem_Click(object sender, EventArgs e)
        {
            RemoveRecordOrder(null, null);
        }

        private void numberOrderTextBox_LostFocus(object sender, EventArgs e)
        {
            ChageNumber(null, null);
        }

        private void dateOrderDateTimePicker_LostFocus(object sender, EventArgs e)
        {
            ChageDate(null, null);
        }
    }
}